<?php
	//这是后台登录的控制器
	class LoginAction extends Action
	{
		public function index()
		{
			$this->display();	
		}	
		
		//调用验证码图片
		public function verify ()
		{
			import('ORG.Util.Image');
    		Image::buildImageVerify(6,1,'png',80,22);
		}
		
		
		//获取表单数据
		public function login()
		{
			if(IS_POST)
			{
				$code = $_SESSION['verify']; 		//获取存储在服务器端SEESION的验证码的值
				$formcode = md5($_POST['code']);	//获取表单提交过来的验证码值
				if($code == $formcode)
				{
					$user = M('user');
					$username = trim(I('username'));		//获取表单提交过来的用户名
					$psw = md5(trim(I('password')));		//获取表单提交过来的密码
					$num = $user->where(array('username' => $username))->find();
					if(empty($num))
					{
						_404('没有这个用户名，请重新登陆');
					}
					else
					{
						if($num[username]==$username && $num[password]==$psw)
						{
							$data = array('userid' => $num['userid'], 'loginip' => get_client_ip(), 'logindate' => time());
							$user->save($data);		//记录用户的登录IP和时间
							session_start();
							$_SESSION['userid'] = $num['userid'];
							$_SESSION['username'] = $num['username'];
							$_SESSION['logindate'] = $num['logindate'];		//用户的登录时间
							$_SESSION['loginip'] = $num['loginip']; 		//用户的登录IP地址
							$this->redirect('Admin/Index/index');
						}
						else
						{
							halt('用户名或者密码错误');	
						}
					}
				
					
				}
				else
				{
					halt ('验证码错误，请重新输入');
				}
				
				
				
			}
			else
			{
				halt('您还未登录，请先登录！');	
			}
			
		}
	}
?>